package com.wanglibao.woodtrade.woodtrade.ui.activity.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.config.NetConfig;
import com.wanglibao.woodtrade.woodtrade.entity.MyCSOrderDetails;
import com.wanglibao.woodtrade.woodtrade.jsonrpc.JsonRPCAsyncTask;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;
import com.wanglibao.woodtrade.woodtrade.ui.activity.OrderInfoEditorActivity;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.MyCSOrderFragment;

import java.util.ArrayList;
import java.util.List;

public class ItemLvMyOrderActivityAdapter extends BaseAdapter {

    private List<MyCSOrderDetails.OrderListBean> mList = new ArrayList<>();

    private Context context;
    MyCSOrderFragment mFragment=new MyCSOrderFragment();
    private LayoutInflater layoutInflater;
    public List<TextView> mTextViews_left_time=new ArrayList<>();
    List<ImageButton> mImageButtons=new ArrayList<>();
    Handler mHandler=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            mFragment.pagrNumber=1;
            mFragment.NetWorking(mFragment.zhuangtai);
            return false;
        }
    });

    public ItemLvMyOrderActivityAdapter(Context context, List<MyCSOrderDetails.OrderListBean> list, MyCSOrderFragment fragment) {
        this.context = context;
        mList=list;
        mFragment=fragment;
        this.layoutInflater = LayoutInflater.from(context);
    }

    public void setList(List<MyCSOrderDetails.OrderListBean> list) {
        mList = list;
        this.notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public MyCSOrderDetails.OrderListBean getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView == null) {
            viewHolder=new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_lv_my_order_activity, null);
            viewHolder.tvStateItemLvMyOrder= (TextView) convertView.findViewById(R.id.tv_state_item_lv_my_order);
            viewHolder.tvPhotoNumberItemLvMyOrder= (TextView) convertView.findViewById(R.id.tv_photoNumber_item_lv_my_order);
            viewHolder.imgvItemLvMyOrderActivity= (SimpleDraweeView) convertView.findViewById(R.id.imgv_item_lv_my_order_activity);
            viewHolder.tvSpmItemLvMyOrder= (TextView) convertView.findViewById(R.id.tv_spm_item_lv_my_order);
            viewHolder.tvGuigeItemLvMyOrder= (TextView) convertView.findViewById(R.id.tv_guige_item_lv_my_order);
            viewHolder.tvShuliangItemLvMyOrder= (TextView) convertView.findViewById(R.id.tv_shuliang_item_lv_my_order);
            viewHolder.tvTotalItemLvMyOrder= (TextView) convertView.findViewById(R.id.tv_total_item_lv_my_order);
            viewHolder.tvDateLvMyOrder= (TextView) convertView.findViewById(R.id.tv_date_lv_my_order);
            viewHolder.tvOrderNumberItemLvMyOrder= (TextView) convertView.findViewById(R.id.tv_orderNumber_item_lv_my_order);
            viewHolder.imgbtDataToLongItemMyOrder= (ImageButton) convertView.findViewById(R.id.imgbt_edit_item_lv_my_order);
            viewHolder.tvShengYuDataLvMyOrder= (TextView) convertView.findViewById(R.id.tv_shengyu_date_lv_my_order);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
            viewHolder.tvStateItemLvMyOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });
            viewHolder.imgbtDataToLongItemMyOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
        initializeViews((MyCSOrderDetails.OrderListBean) getItem(position), (ViewHolder) convertView.getTag(),position);
        return convertView;
    }
    String Status0="0";
    String Status1="1";
    String Status2="2";
    String Status3="3";
    String Delay="0";
    String Delay1="1";
    private void initializeViews(final MyCSOrderDetails.OrderListBean entitiy, final ViewHolder holder, final int position) {
        holder.tvPhotoNumberItemLvMyOrder.setText("联系方式:"+entitiy.getUser_name());
        holder.imgvItemLvMyOrderActivity.setImageURI(Uri.parse(entitiy.getGoods_img()));
        holder.tvSpmItemLvMyOrder.setText(entitiy.getTitle());
        holder.tvGuigeItemLvMyOrder.setText("规格："+entitiy.getSize());
        holder.tvShuliangItemLvMyOrder.setText("数量："+entitiy.getGoods_num());
        holder.tvTotalItemLvMyOrder.setText("合计："+entitiy.getPay_fee()+"元");
        holder.tvShengYuDataLvMyOrder.setTag(position);
        holder.tvDateLvMyOrder.setText("订单时间："+entitiy.getCreate_time().substring(0,10));
        holder.tvOrderNumberItemLvMyOrder.setText("订单号："+entitiy.getOrder_sn());
        if(entitiy.getOrder_status().equals(Status0)){
            holder.tvStateItemLvMyOrder.setText("待确认");
            holder.imgbtDataToLongItemMyOrder.setBackgroundResource(R.mipmap.ic_dingdan_queren);
            holder.imgbtDataToLongItemMyOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, OrderInfoEditorActivity.class);
                    intent.putExtra("order_id",entitiy.getOrder_id());
                    intent.putExtra("store_id",entitiy.getStore_id());
                    context.startActivity(intent);
                }
            });
            mTextViews_left_time.add(holder.tvShengYuDataLvMyOrder);
            holder.tvShengYuDataLvMyOrder.setText(convertTimeFormat(Long.parseLong(entitiy.getC_time())));
        }else if(entitiy.getOrder_status().equals(Status1)){
            holder.tvStateItemLvMyOrder.setText("已确认");
            System.out.println("Delay"+entitiy.getDelay());
            if(entitiy.getDelay().equals(Delay)){
                holder.imgbtDataToLongItemMyOrder.setBackgroundResource(R.mipmap.ic_dingdan_yanchang);
                holder.imgbtDataToLongItemMyOrder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        holder.imgbtDataToLongItemMyOrder.setTag(position);
                        mImageButtons.add(holder.imgbtDataToLongItemMyOrder);
                       showDialog(entitiy,holder,position);
                    }
                });
                holder.imgbtDataToLongItemMyOrder.setBackgroundResource(R.mipmap.ic_dingdan_yanchang);
            }else if(Delay1.equals(entitiy.getDelay())) {
                holder.imgbtDataToLongItemMyOrder.setBackgroundResource(R.mipmap.ic_dingdan_yiyanchang);
            }
            mTextViews_left_time.add(holder.tvShengYuDataLvMyOrder);
            holder.tvShengYuDataLvMyOrder.setText(convertTimeFormat(Long.parseLong(entitiy.getC_time())));
        }else if(entitiy.getOrder_status().equals(Status2)){
            holder.tvStateItemLvMyOrder.setText("已完成");
            holder.imgbtDataToLongItemMyOrder.setBackgroundResource(R.color.white);
            holder.tvShengYuDataLvMyOrder.setText("");
        }else if(entitiy.getOrder_status().equals(Status3)){
            holder.tvStateItemLvMyOrder.setText("已失效");
            holder.imgbtDataToLongItemMyOrder.setBackgroundResource(R.color.white);
            holder.tvShengYuDataLvMyOrder.setText("");
        }
    }
    Dialog dialog;

    private void showDialog(final MyCSOrderDetails.OrderListBean entitiy,final ViewHolder holder, final int position) {
        dialog = new Dialog(context, R.style.dialog_bgTransparent);
        dialog.setCanceledOnTouchOutside(true);
        dialog.show();
        dialog.getWindow().setContentView(R.layout.dialog_tel);
        TextView tv_title = (TextView) dialog.findViewById(R.id.tv_dialog_title);
        tv_title.setText("温馨提示");
        TextView tv_contact = (TextView) dialog.findViewById(R.id.tv_dialog_contact);
        tv_contact.setText("是否将订单时间延长2天");
        TextView tv_cancle = (TextView) dialog.findViewById(R.id.tv_dialog_tel_cancle);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView tv_tel = (TextView) dialog.findViewById(R.id.tv_dialog_call);
        tv_tel.setText("确定");
        tv_tel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                JsonRPCAsyncTask jsonRPCAsyncTask = new JsonRPCAsyncTask((Activity) context, NetConfig.SERVER + NetConfig.TYPE_ORDER,
                        NetConfig.delay_order, mHandler, "jsonObject", NetConfig.TaskId.delay_order, new Object[]{entitiy.getOrder_id(), new UserImpl(context).getUser().token});
                jsonRPCAsyncTask.execute(new Void[]{});
            }
        });

    }
     public class ViewHolder {

        TextView tvDianmingItemLvMyOrder;
        TextView tvPhotoNumberItemLvMyOrder;
        SimpleDraweeView imgvItemLvMyOrderActivity;
        TextView tvSpmItemLvMyOrder;
        TextView tvGuigeItemLvMyOrder;
        TextView tvShuliangItemLvMyOrder;
        TextView tvTotalItemLvMyOrder;
        TextView tvDateLvMyOrder;
        TextView tvOrderNumberItemLvMyOrder;
        TextView tvStateItemLvMyOrder;
         ImageButton imgbtDataToLongItemMyOrder;
         TextView tvShengYuDataLvMyOrder;
    }
    //把毫秒值转换成时.分.秒格式
    public String convertTimeFormat(long timeAll){
        long now=System.currentTimeMillis()-(timeAll*1000);
        int hour= (int) (timeAll/(1000*60*60));
        int timeleft1= (int) (timeAll%(1000*60*60));
        int minute= timeleft1/(1000*60);
        int timeleft2= (int) (timeAll%(1000*60));
        int second= timeleft2/1000;
        return "剩余时间："+hour+"时"+minute+"分"+second+"秒";
    }
}
